// hyb_yl/krecovery/pages/order_list/order_list.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: 0,
    tablist: ['全部', '待支付','已支付', '已结束'],
    page:0,
    statuss:"",
    list:[]
  },
  choose_tab: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      status: index,
      statuss:index-1,
      page:0,
      list:[]
    })
    this.getOrder();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getOrder();
    var typs = options.typs;
    if(typs != undefined && typs != 'undefined')
    {
      that.setData({
        typs:typs
      })
    }
    app.util.request({
      'url': 'entry/wxapp/index.base',
      success: function (res) {
        var se = res.data.ztcolor
        wx.setNavigationBarColor({
          frontColor: '#ffffff',
          backgroundColor: se,
        })
      },
      fail: function (err) {
        console.log(err)
      },
    })
  },
  // 确认收货
  toreceive:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    app.util.reqest({
      'url':"entry/wxapp/follow.receive",
      data:{
        id:id
      },
      success:function(res)
      {
        that.setData({
          page:0,
          list:[]
        })
        that.getOrder();
      }
    })
  },
  getOrder: function () {
    var that = this;
    var lists = that.data.list;
    app.util.request({
      'url': "entry/wxapp/follow.user_fuwu",
      data: {
        openid: wx.getStorageSync('openid'),
        page: that.data.page,
        status: that.data.status
      },
      success: function (res) {
        var page = that.data.page + 1;

        that.setData({
          page: page,
          list: lists.concat(res.data)
        })
      }
    })
  },
  // 支付订单
  topay:function(e){
    var that = this;
    var money = e.currentTarget.dataset.money;
    var back_orser = e.currentTarget.dataset.back_orser;
    app.util.request({
      'url': 'entry/wxapp/huiyuan.pay',
      header: {
        "Content-Type": "application/xml"
      },
      method: "GET",
      data: {
        openid: wx.getStorageSync('openid'),
        payprice: money,
        ordersn:back_orser
      },
      success: function (res) {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr, //字符串随机数
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          success: function (res) {},
          complete: function (res) {
            console.log(res)
            if (res.errMsg != 'requestPayment:fail cancel') {
              app.util.request({
                'url': 'entry/wxapp/index.update_service',
                data: {
                  openid: wx.getStorageSync('openid'),
                  back_orser: back_orser,
                  
                },
                success: function (payres) {
                  wx.showToast({
                    title: '支付成功!',
                  })
                  
                  that.setData({
                    list:[],
                    page:0
                  })
                  that.getOrder();
                },
              })
            }
          }
        })
      }
    })
  },
  // 跳转详情
  todetail:function(e){
    var that = this;
    var id = e.currentTarget.dataset.s_id;
    wx.navigateTo({
      url: '/hyb_yl/backstageServices/pages/vips/vips?id='+id,
    })
  },
  del_order:function(e){
    var that = this;;
    var id = e.currentTarget.dataset.s_id;
    wx.showModal({
      title: '提示',
      content:"确认要删除该订单吗",
      success:function(con)
      {
        if(con.confirm)
        {
          app.util.request({
            'url':"enrey/wxapp/index.del_server",
            data:{
              id:id
            },
            success:function(res)
            {
              wx.showToast({
                title: '删除成功',
              })
              that.setData({
                page:0,
                list:[]
              })
              that.getOrder();
            }
          })
        }
      }
    })
  },
  // 支付订单
  topays:function(e){
    var that = this;
    var orders = e.currentTarget.dataset.orders;
    var money = e.currentTarget.dataset.money;
    var id = e.currentTarget.dataset.id;
    var zid = e.currentTarget.dataset.zid;
    if(money == '0.00' || money == '0' || money == 0)
    {
      app.util.request({
        'url': 'entry/wxapp/follow.update_fuwu',
        data: {
          back_orser: orders,
        },
        success: function (res) {
          console.log(res)

        }
      })
      //模板消息提醒
      app.util.request({
        'url': 'entry/wxapp/zhuanjia.getdocmbtxing',
        data: {
          id: id,
          near: '术后康复',
          zid: zid
        },
        success: function (res) {
          console.log(res)
          that.setData({
            page:0,
            list:[],
          })
          that.getOrder();
        }
      })
    }else{
      app.util.request({
        'url': 'entry/wxapp/follow.pay_fuwu',
        header: {
          "Content-Type": "application/xml"
        },
        method: "GET",
        data: {
          openid: wx.getStorageSync('openid'),
          money: money,
          orders: orders,
        },
        success: function (res) {
          wx.requestPayment({
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr, //字符串随机数
            'package': res.data.package,
            'signType': res.data.signType,
            'paySign': res.data.paySign,
            success: function (res) {
              console.log(res)
  
            },
            fail: function (res) {
              console.log(res)
  
            },
            'complete': function (res) {
              //更新问诊时间
              if (res.errMsg == 'requestPayment:ok') {
               
                app.util.request({
                  'url': 'entry/wxapp/follow.update_fuwu',
                  data: {
                    back_orser: orders,
                  },
                  success: function (res) {
                    console.log(res)
  
                  }
                })
                //模板消息提醒
                app.util.request({
                  'url': 'entry/wxapp/zhuanjia.getdocmbtxing',
                  data: {
                    id: id,
                    near: '术后康复',
                    zid: zid
                  },
                  success: function (res) {
                    console.log(res)
                    that.setData({
                      page:0,
                      list:[],
                    })
                    that.getOrder();
                  }
                })
              }
            }
          })
        }
      })
    }
    
  },
  // 取消订单
  quxiaos:function(e){
    var that = this;
    var back_orser = e.currentTarget.dataset.orders;
    app.util.request({
      'url':"entry/wxapp/follow.fuwu_quxiao",
      data:{
        back_orser:back_orser
      },
      success:function(res){
        that.setData({
          page:0,
          list:[]
        })
        that.getOrder();
      }
    })
  },
  // 跳转详情 
  todetails:function(e){
    var that = this;
    var back_orser = e.currentTarget.dataset.back_orser;
    wx.navigateTo({
      url: '/hyb_yl/huanzhe/pages/fwb_order_detail/fwb_order_detail?back_orser='+back_orser+'&key_words=fuwu',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getOrder();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})